import { Service, Inject } from "typedi";
import { HomeService } from "../HomeService";
import { HomeDao } from "../../dao/HomeDao";
import { UserDao } from "../../dao/UserDao";
import { DynamicDao } from "../../dao/DynamicDao";
import { HomeDataModel,LikeThinkModel,ThinkInfoModel,CollectionThinkModel,setRecentVisitModel } from "../../model/HomeModel";

import { ThinkEntity } from "../../entity/ThinkEntity";
import { UserEntity } from "../../entity/UserEntity";
import { BannerEntity } from "../../entity/BannerEntity";

@Service()
export class HomeServiceImpl implements HomeService {

  @Inject()
  private HomeDao!: HomeDao;
  @Inject()
  private DynamicDao!: DynamicDao;
  @Inject()
  private UserDao!: UserDao;
  

  public async getAllHomeData(params: HomeDataModel): Promise<any> {
    
    let thinkData:ThinkEntity[] = await this.getHomeThinkData(params);
    let bannerData:BannerEntity[] = [];
    bannerData = await this.getBanner(params.banner_type)
    await Promise.all(
      thinkData.map(async (e: ThinkEntity, thinkIndex: number)=>{
         return await this.changeThink(e,params.user_Id)
      })
    ).then((res:ThinkEntity[])=>{
    thinkData = res;
    })
    return { thinkData,bannerData };
  }

  public async getBanner(banner_type: number): Promise<any> {
    
    return  await this.HomeDao.getBanner(banner_type);
     
  }
 
  public async setRecentVisit(params: setRecentVisitModel): Promise<any> {
    let user:UserEntity|undefined = await this.UserDao.getUserInfoDao(parseInt(params.user_Id));
    let RecentVisitList : string[];
    switch(params.recentVisit_Type){
      case('0'):
      RecentVisitList =  JSON.parse(user!.user_UserInfo.user_RecentVisitList)
      if( RecentVisitList.indexOf(params.thing_Id) == -1){
        RecentVisitList.unshift(params.thing_Id.toString())
      }else{
        RecentVisitList.splice(RecentVisitList.indexOf(params.thing_Id),1)
        RecentVisitList.unshift(params.thing_Id.toString())
      }

      
      await this.HomeDao.addWatchTypeDao(parseInt(params.thing_Id));
      return await this.UserDao.setRecentVisitDao(parseInt(params.user_Id),JSON.stringify(RecentVisitList))
      case('1'):
      RecentVisitList =  JSON.parse(user!.user_UserInfo.user_RecentVisitDynamicList)
      if( RecentVisitList.indexOf(params.thing_Id) == -1){
        RecentVisitList.unshift(params.thing_Id.toString())
      }else{
        RecentVisitList.splice(RecentVisitList.indexOf(params.thing_Id),1)
        RecentVisitList.unshift(params.thing_Id.toString())
      }

      
      await this.DynamicDao.addWatchTypeDao(parseInt(params.thing_Id));
      return await this.UserDao.setRecentVisitDynamicDao(parseInt(params.user_Id),JSON.stringify(RecentVisitList))
      case('2'):
      RecentVisitList =  JSON.parse(user!.user_UserInfo.user_RecentVisitUserList)
      if( RecentVisitList.indexOf(params.thing_Id) == -1){
        RecentVisitList.unshift(params.thing_Id.toString())
      }else{
        RecentVisitList.splice(RecentVisitList.indexOf(params.thing_Id),1)
        RecentVisitList.unshift(params.thing_Id.toString())
      }

      
      return await this.UserDao.setRecentVisitUserDao(parseInt(params.user_Id),JSON.stringify(RecentVisitList))
      
    }
     
  }
  
  public async getThinkInfo(params: ThinkInfoModel): Promise<any> {
    
    let think:ThinkEntity | undefined ;
   
    if(params.think_Category == 0){
      think =   await this.HomeDao.getThinkInfo(params);
    }else if(params.think_Category == 2){
      think =   await this.HomeDao.getThinkPaint(params);
    }else{
      think = await this.HomeDao.getThinkAncient(params)
    }
    return await this.changeThink(think!,params.user_Id);
     
  }
  
  public async updateApp(id: number): Promise<any> {
    
    return  await this.HomeDao.getUpdateData(id);
     
  }
  
  public async getHomeThinkData(params: HomeDataModel): Promise<any> {

    let thinkList:ThinkEntity[] =  await this.HomeDao.getHomeThinkDao(
      params.think_Page,
      params.think_Count
    );
    return  await Promise.all(
      thinkList.map(async (e: ThinkEntity, thinkIndex: number)=>{
         return await this.changeThink(e,params.user_Id)
      })
    ).then((res:ThinkEntity[])=>{
    return res;
    })
  }
  public async changeLikeThink(params:LikeThinkModel): Promise<any> {
    let userInfo:UserEntity | undefined = await this.UserDao.getUserInfoDao(params.user_Id);
    let like_List: string[]  = JSON.parse(userInfo!.user_UserInfo.user_LikeList);
    if(params.isLike=='true'){
      like_List[like_List.length] = (params.think_Id).toString();
    }else{
      like_List.some((element: string, i: number) => {
        if (element ==(params.think_Id).toString()) {
          like_List.splice(i, 1);
          return true;
        }
      });
    }
    await this.HomeDao.changeLikeThinkDao(params.think_Id,params.isLike)
    return await this.UserDao.setLikeListDao(params.user_Id, JSON.stringify(like_List));
  }


  public async changeCollectionThink(params:CollectionThinkModel): Promise<any> {
    let userInfo:UserEntity | undefined = await this.UserDao.getUserInfoDao(params.user_Id);
    let collection_List: string[]  = JSON.parse(userInfo!.user_UserInfo.user_CollectionList);
    if(params.isCollection=='true'){
      collection_List[collection_List.length] = (params.think_Id).toString();
    }else{
      collection_List.some((element: string, i: number) => {
        if (element ==(params.think_Id).toString()) {
          collection_List.splice(i, 1);
          return true;
        }
      });
    }
    await this.HomeDao.changeCollectionThinkDao(params.think_Id,params.isCollection)
    return await this.UserDao.setCollectionListDao(params.user_Id, JSON.stringify(collection_List));
  }


  public async changeThink(think:ThinkEntity,userId:number):Promise<any>{

    let userInfo: UserEntity | undefined = await this.UserDao.getUserInfoDao(
     userId
    );
    JSON.parse(userInfo!.user_UserInfo.user_LikeList).some((e:string)=>{
      if(parseInt(e) == think.id){
        think.isLike = true;
          return true;
      }
    })
    JSON.parse(userInfo!.user_UserInfo.user_CollectionList).some((e:string)=>{
      if(parseInt(e) == think.id){
        think.isCollection = true;
          return true;
      }
    })
    if(think.think_Info){
      think.think_Info.think_Info = JSON.parse(think.think_Info.think_Info);
      think.think_Info.think_TagList = JSON.parse(think.think_Info.think_TagList);
    }
    if(think.think_Paint){
      think.think_Paint.paint_Info = JSON.parse(think.think_Paint.paint_Info);
      think.think_Paint.paint_TagList = JSON.parse(think.think_Paint.paint_TagList);
    }
    if(think.think_Ancient){
      think.think_Ancient.ancient_Info = JSON.parse(think.think_Ancient.ancient_Info);
      think.think_Ancient.ancient_TagList = JSON.parse(think.think_Ancient.ancient_TagList);
    }
    return think;
}
}
